package com.uraurora.crescent.function;

import com.google.common.cache.Cache;
import com.uraurora.crescent.function.functools.memoizer.MemoizerFunc3;
import com.uraurora.crescent.tuple.ITuple;

import java.util.Objects;
import java.util.concurrent.Future;

/**
 * 3个参数具有返回值的函数式接口
 * @author ：Sei
 * @param <T1>
 * @param <T2>
 * @param <T3>
 * @param <RT>
 */
@FunctionalInterface
public interface Func3<T1, T2, T3, RT> {

    RT on(T1 t1, T2 t2, T3 t3);

    default <V> Func3<T1, T2, T3, V> after(Func1<? super RT, ? extends V> after) {
        Objects.requireNonNull(after);
        return (T1 t1, T2 t2, T3 t3) -> after.on(on(t1, t2, t3));
    }

    //<editor-fold desc="柯里化与反柯里化">
    default Func2<T2, T3, RT> curry1(T1 t1){
        return (t2, t3) -> on(t1, t2, t3);
    }

    default Func2<T1, T3, RT> curry2(T2 t2){
        return (t1, t3) -> on(t1, t2, t3);
    }

    default Func2<T1, T2, RT> curry3(T3 t3){
        return (t1, t2) -> on(t1, t2, t3);
    }

    default Func1<T3, RT> curry(T1 t1, T2 t2){
        return t3 -> on(t1, t2, t3);
    }
    //</editor-fold>

    //<editor-fold desc="记忆化">
    default MemoizerFunc3<T1, T2, T3, RT> memoize(){
        return new MemoizerFunc3<>(this);
    }

    default MemoizerFunc3<T1, T2, T3, RT> memoize(Cache<ITuple, Future<RT>> cache){
        return new MemoizerFunc3<>(this, cache);
    }
    //</editor-fold>


}
